public class BinarySearchTree {
    //二叉搜索树
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val){
            this.val=val;
        }

    }
    protected TreeNode root=null;
    //插入
    public void insert(int key){
        if(root==null){
            root=new TreeNode(key);
            return;
        }
        TreeNode cur=root;
        TreeNode parent=null;
        TreeNode node=new TreeNode(key);
        while(cur!=null){
            if(cur.val<key){
                parent=cur;
                cur=cur.right;
            } else if (cur.val>key) {
                parent=cur;
                cur=cur.left;
            }else{
                //不能插入两个相同的数据
                return;
            }
        }
        if(parent.val>key){
            parent.left=node;
        }else{
            parent.right=node;
        }

    }
    //查找
    public boolean search(int key){
        TreeNode cur=root;
        while(cur!=null){
            if(cur.val<key){
                cur=cur.right;
            }else if(cur.val>key){
                cur=cur.left;
            }else{
                return true;
            }
        }
        return false;
    }
}
